//
//  MSWalletController.m
//  觅尚
//
//  Created by No.8 on 2019/4/1.
//  Copyright © 2019 No.8. All rights reserved.
//

#import "MSWalletController.h"
#import "MSWalletCell.h"
#import "MSIncomeDetailsController.h"
#import "MSWithdrawalController.h"
#import "MSBankWithdrawalController.h"

@interface MSWalletController ()<UITableViewDelegate,UITableViewDataSource>
{
    CustomNavigationBar *naviagtionBar;
}
/** 表视图*/
@property(nonatomic,strong)UITableView *tableView;


/** 今日余额*/
@property(nonatomic,strong)UILabel * TodayevaluationLabel;


/** 账户余额*/
@property(nonatomic,strong)UILabel * AccountbalanceLabel;


@end

@implementation MSWalletController

- (int)navBarBottom {
    if ([WRNavigationBar isIphoneX]) {
        return 88;
    } else {
        return 64;
    }
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    self.navigationController.navigationBar.hidden = YES;
    dispatch_async(dispatch_get_main_queue(), ^{
        
        [self ReloadData];
        
    });
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:YES];
    self.navigationController.navigationBar.hidden = YES;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = backgroundcolor;
    
    [self CreatView];
    
    
    [self tableView];
    
    
}
- (void)CreatView{
    UIView * HeaderView = [[UIView alloc] init];
    HeaderView.x = 0;
    HeaderView.y = 0;
    HeaderView.width = ScreenWidth;
    HeaderView.backgroundColor = backgroundcolor;
    
    
    
    UIView * ImageView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 900 * px)];
    UIImage*image = [UIImage imageNamed:@"wodetoutu"];
    ImageView.layer.contents = (__bridge id)image.CGImage;
    /** 设置对齐模式*/
    ImageView.layer.contentsGravity = kCAGravityResize;
    [HeaderView addSubview:ImageView];
    
    
    /** 返回按钮*/
    UIButton * backButton = [[UIButton alloc] init];
    if ([WRNavigationBar isIphoneX]) {
        backButton.frame = CGRectMake(52 * px, 55, 30, 30);
    }else{
        backButton.frame = CGRectMake(52 * px, 30, 30, 30);
        
    }
    [backButton  setImage:[UIImage imageNamed:@"左箭头"] forState:UIControlStateNormal];
    [backButton  addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    [HeaderView  addSubview:backButton];
    
    /** 个人主页标题*/
    UILabel * titlelabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth * 0.5 - 100 * px,backButton.y, 200 * px, 60 * px)];
    titlelabel.textColor = [UIColor whiteColor];
    titlelabel.text = @"钱包";
    titlelabel.textAlignment = NSTextAlignmentCenter;
    titlelabel.font = [UIFont systemFontOfSize:SYRealValue(38 / 2)];
    [HeaderView addSubview:titlelabel];
    
    
    /** 今日评价收入*/
    UILabel * TodayevaluationTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth * 0.25, CGRectGetMaxY(titlelabel.frame) + 100 * px, ScreenWidth * 0.5, 60 * px)];
    TodayevaluationTitleLabel.textColor = [UIColor whiteColor];
    TodayevaluationTitleLabel.text = @"今日评价收入(元)";
    TodayevaluationTitleLabel.textAlignment = NSTextAlignmentCenter;
    TodayevaluationTitleLabel.font = [UIFont systemFontOfSize:SYRealValue(26 / 2)];
    [HeaderView addSubview:TodayevaluationTitleLabel];
    
    
    /** 今日评价收入*/
    UILabel * TodayevaluationLabel = [[UILabel alloc] initWithFrame:CGRectMake(TodayevaluationTitleLabel.x, CGRectGetMaxY(TodayevaluationTitleLabel.frame) + 40 * px, TodayevaluationTitleLabel.width, TodayevaluationTitleLabel.height)];
    TodayevaluationLabel.textColor = [UIColor whiteColor];
    TodayevaluationLabel.text = @"4527.46";
    TodayevaluationLabel.textAlignment = NSTextAlignmentCenter;
    TodayevaluationLabel.font = [UIFont boldSystemFontOfSize:SYRealValue(35 / 2)];
    [HeaderView addSubview:TodayevaluationLabel];
    self.TodayevaluationLabel = TodayevaluationLabel;
    
    
    /** 账户余额*/
    UILabel * AccountbalanceLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(TodayevaluationLabel.frame) + 100 * px, ScreenWidth * 0.5, 60 * px)];
    AccountbalanceLabel.textColor = TodayevaluationLabel.textColor;
    AccountbalanceLabel.text = @"3456.23";
    AccountbalanceLabel.textAlignment = TodayevaluationLabel.textAlignment;
    AccountbalanceLabel.font = TodayevaluationLabel.font;
    [HeaderView addSubview:AccountbalanceLabel];
    
    
    /** 账户余额*/
    UILabel * AccountbalanceTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(AccountbalanceLabel.x, CGRectGetMaxY(AccountbalanceLabel.frame) + 40 * px, AccountbalanceLabel.width, AccountbalanceLabel.height)];
    AccountbalanceTitleLabel.textColor = TodayevaluationTitleLabel.textColor;
    AccountbalanceTitleLabel.text = @"账户余额";
    AccountbalanceTitleLabel.textAlignment = TodayevaluationTitleLabel.textAlignment;
    AccountbalanceTitleLabel.font = TodayevaluationTitleLabel.font;
    [HeaderView addSubview:AccountbalanceTitleLabel];
    self.AccountbalanceLabel = AccountbalanceLabel;
    
    
    UIView * lineView = [[UIView alloc] initWithFrame:CGRectMake(ScreenWidth * 0.5 - 0.5, AccountbalanceLabel.y - 10 * px, 1, AccountbalanceLabel.height + AccountbalanceTitleLabel.height + 50 * px)];
    lineView.backgroundColor = WYCRGBColor(165, 117, 172);
    [HeaderView addSubview:lineView];
    
    
    /** 提现按钮*/
    UIButton * WithdrawalButton = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth * 0.5 + 150 * px, AccountbalanceLabel.y  + 30 * px, ScreenWidth * 0.5 - 300 * px, 80 * px)];
    [WithdrawalButton setTitle:@"提现" forState:UIControlStateNormal];
    WithdrawalButton.titleLabel.font = AccountbalanceTitleLabel.font;
    [WithdrawalButton  setTitleColor:FontColor forState:UIControlStateNormal];
    WithdrawalButton.layer.cornerRadius = 40 * px;
    WithdrawalButton.backgroundColor = TodayevaluationTitleLabel.textColor;
    [WithdrawalButton  addTarget:self action:@selector(WithdrawalButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
    [HeaderView  addSubview:WithdrawalButton];
    
    
    HeaderView.height = CGRectGetMaxY(ImageView.frame) + 60 * px;
    
    [self.view addSubview:HeaderView];
    
    self.tableView.tableHeaderView = HeaderView;
    
    
}

- (void)ReloadData{
    
    NSDictionary * params = @{@"uid":[WYCAccountTool unarchiveuid]};
    [HTTPRequest POST:MSgetRecord params:params succeed:^(id responseObject) {
        WYCLog(@"=-========%@",responseObject);
        if (CodeStr) {
//            if (responseObject[@"data"][@"incomeMoney"] == nil || responseObject[@"data"][@"incomeMoney"] == NULL || [responseObject[@"data"][@"incomeMoney"] isKindOfClass:[NSNull class]] || [[responseObject[@"data"][@"incomeMoney"] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] == 0){
//
//                self.TodayevaluationLabel.text = @"0";
//            }else{
                self.TodayevaluationLabel.text = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"incomeMoney"]];
//            }
            
//            if (responseObject[@"data"][@"AccountMoney"] == nil || responseObject[@"data"][@"AccountMoney"] == NULL || [responseObject[@"data"][@"AccountMoney"] isKindOfClass:[NSNull class]] || [[responseObject[@"data"][@"AccountMoney"] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] == 0){
//                self.AccountbalanceLabel.text = @"0";
//            }else{
                self.AccountbalanceLabel.text = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"AccountMoney"]];
//            }
        }else{
            
            [MBManager showBriefAlert:responseObject[@"msg"]];
        }
        
    } failure:^(NSError *error) {
        [MBManager showBriefAlert:Failed];
    }];
}

//提现跳转
- (void)WithdrawalButtonClick{
    MSBankWithdrawalController * BankWithdrawalVC = [[MSBankWithdrawalController alloc] init];
    [self.navigationController pushViewController:BankWithdrawalVC animated:NO];
    
}
/** 表视图*/
- (UITableView *)tableView{
    self.extendedLayoutIncludesOpaqueBars = YES;
    if (!_tableView) {
        
        if (@available(iOS 11.0, *)) {
            
            UIScrollView.appearance.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            
        } else {
            
            self.automaticallyAdjustsScrollViewInsets = NO;
            
        }
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:self.tableView];
    }
    return _tableView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 150 * px;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    MSWalletCell * cell = [MSWalletCell cellWithTableView:tableView];
    tableView.backgroundColor = [UIColor clearColor];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    //无色
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
   
    
    if (indexPath.row == 0) {
        /** 收入图标*/
        UIImageView * incomeIMG = [[UIImageView alloc] initWithFrame:CGRectMake(40 * px, 30 * px, 60 * px, 60 * px)];
        incomeIMG.image = [UIImage imageNamed:@"srjl_icon"];
        [cell.contentView addSubview:incomeIMG];
        
        
        /** 收入标题*/
        UILabel * incomeLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(incomeIMG.frame) + 40 * px, incomeIMG.y, 200 * px, 60 * px)];
        incomeLabel.textColor = [UIColor whiteColor];
        incomeLabel.text = @"收入记录";
        incomeLabel.textAlignment = NSTextAlignmentLeft;
        incomeLabel.font = [UIFont systemFontOfSize:SYRealValue(30 / 2)];
        [cell.contentView addSubview:incomeLabel];
        
        
        /** 分割线*/
        UIView * lineView = [[UILabel alloc] initWithFrame:CGRectMake(incomeIMG.x, cell.height - 1, ScreenWidth - 80 * px, 1)];
        lineView.backgroundColor = BackViewColor;
        [cell.contentView addSubview:lineView];
    }else{
        /** 收入图标*/
        UIImageView * WithdrawalIMG = [[UIImageView alloc] initWithFrame:CGRectMake(40 * px, 30 * px, 60 * px, 60 * px)];
        WithdrawalIMG.image = [UIImage imageNamed:@"txjl_icon"];
        [cell.contentView addSubview:WithdrawalIMG];
        
        
        /** 收入标题*/
        UILabel * incomeLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(WithdrawalIMG.frame) + 40 * px, WithdrawalIMG.y, 200 * px, WithdrawalIMG.height)];
        incomeLabel.textColor = [UIColor whiteColor];
        incomeLabel.text = @"提现记录";
        incomeLabel.textAlignment = NSTextAlignmentLeft;
        incomeLabel.font = [UIFont systemFontOfSize:SYRealValue(30 / 2)];
        [cell.contentView addSubview:incomeLabel];
        
        
        /** 分割线*/
        UIView * lineView = [[UILabel alloc] initWithFrame:CGRectMake(WithdrawalIMG.x, cell.height - 1, ScreenWidth - 80 * px, 1)];
        lineView.backgroundColor = BackViewColor;
        [cell.contentView addSubview:lineView];
        
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        MSIncomeDetailsController * IncomeDetailsVC = [[MSIncomeDetailsController alloc] init];
        [self.navigationController pushViewController:IncomeDetailsVC animated:nil];
    }else{
        
        MSWithdrawalController * WithdrawalVC = [[MSWithdrawalController alloc] init];
        [self.navigationController pushViewController:WithdrawalVC animated:nil];
    }
    
}
- (void)backClick{
    
    [self.navigationController popViewControllerAnimated:NO];
}
@end
